package com.example.covid19.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.Clastab;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Classes;
import com.example.covid19.entity.SelectClass.CrmDto;
import com.example.covid19.mapper.ClassesMapper;
import com.example.covid19.mapper.CollegeMapper;
import com.example.covid19.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    CollegeMapper collegeMapper;
    @Autowired
    ClassesService classesService;
    @RequestMapping("/getlist")
    public Result getlist(){
        return Result.success(200,"ok",classesMapper.getlist());
    }
    @RequestMapping("/getCrm")
    public Result getCrm(){
        List<Clastab> crmDtoList = classesMapper.getlist();
        JSONArray treeData = new JSONArray();

        for (Clastab clastab : crmDtoList) {
            JSONObject collegeObj = findObjectByKey(treeData, "label", clastab.getCname());

            if (collegeObj == null) {
                collegeObj = new JSONObject();
                collegeObj.set("label", clastab.getCname());
                collegeObj.set("value", clastab.getCname());
                collegeObj.set("key", clastab.getCname());
                collegeObj.set("children", new JSONArray());
                treeData.add(collegeObj);
            }

            JSONArray departments = collegeObj.getJSONArray("children");
            JSONObject departmentObj = findObjectByKey(departments, "label", clastab.getRname());

            if (departmentObj == null) {
                departmentObj = new JSONObject();
                departmentObj.set("label", clastab.getRname());
                departmentObj.set("value", clastab.getRname());
                departmentObj.set("key", clastab.getCname() + "-" + clastab.getRname());
                departmentObj.set("children", new JSONArray());
                departments.add(departmentObj);
            }

            JSONArray majors = departmentObj.getJSONArray("children");
            JSONObject majorObj = findObjectByKey(majors, "label", clastab.getMajeurname());

            if (majorObj == null) {
                majorObj = new JSONObject();
                majorObj.set("label", clastab.getMajeurname());
                majorObj.set("value", clastab.getMajeurname());
                majorObj.set("key", clastab.getCname() + "-" + clastab.getRname() + "-" + clastab.getMajeurname());
                majorObj.set("children", new JSONArray());
                majors.add(majorObj);
            }

            JSONArray classes = majorObj.getJSONArray("children");
            JSONObject classObj = new JSONObject();
            classObj.set("label", clastab.getClname());
            classObj.set("value", clastab.getCcid());
            classObj.set("key", clastab.getCcid());
            classes.add(classObj);
        }

        return Result.success(200, "ok", treeData);
    }

    /**
     *
     * @param name
     * @param mid
     * @return 添加学院
     */
    @RequestMapping("AddClass")
    public Result AddClass(@RequestParam("name")String name,@RequestParam("mid")Integer mid){
        Classes classes = new Classes();
        classes.setMid(mid);
        classes.setClname(name);
        boolean flag = classesService.save(classes);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }

    /**
     *
     * @param id
     * @return删除班级
     */
    @RequestMapping("deleteClass")
    public Result deleteClass(@RequestParam("id")Integer id){
        Classes classes = classesService.getOne(new QueryWrapper<Classes>().eq("ccid",id));
        boolean flag = classesService.removeById(classes);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }

    /**
     *
     * @param id
     * @param mid
     * @param name
     * @return 修改班级
     */
    @RequestMapping("editClass")
    public Result editClass(@RequestParam("id")Integer id,@RequestParam("mid")Integer mid,@RequestParam("name")String name){
        Classes classes = classesService.getOne(new QueryWrapper<Classes>().eq("ccid",id));
        classes.setMid(mid);
        classes.setClname(name);
        boolean flag = classesService.updateById(classes);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }
    private JSONObject findObjectByKey(JSONArray jsonArray, String key, String value) {
        for (Object obj : jsonArray) {
            JSONObject jsonObj = (JSONObject) obj;
            if (jsonObj.getStr(key).equals(value)) {
                return jsonObj;
            }
        }
        return null;
    }

}
